package com.hzbank.generate.controller;

import com.hzbank.generate.entity.AccountDetails;
import com.hzbank.generate.entity.AccountInfo;
import com.hzbank.generate.entity.vo.DetailList;
import com.hzbank.generate.service.InterfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/details")
public class DetailsController {
    @Autowired
    private InterfaceService interfaceService;

    @RequestMapping("/one")
    @ResponseBody
    private List<AccountDetails> selectOne(){

        //List<AccountDetails> accountDetails = interfaceService.selectDetailsByPrimaryKey("2020122514022185083");
        return interfaceService.selectDetailsByPrimaryKey("2020122514022185083");
    }


    @RequestMapping("/list1")
    public List<Map<String,Object>> list1(@RequestBody DetailList detailList) {
        List<AccountDetails> detailsList = interfaceService.list1(detailList);
        List<Map<String,Object>> list = new ArrayList<>();
        //摘要
        String[] memo = {"支付宝","财付通","小米","其他"};
        for(AccountDetails detail:detailsList){
            Map<String,Object> map = new HashMap<>();
            map.put("TRAN_DT",detail.getTranDt());
            map.put("TRAN_SEQ",detail.getTranSeq());
            map.put("TRAN_AMT",detail.getTranAmt());
            map.put("CCY",detail.getCcy());
            map.put("MEMO",memo);
            list.add(map);
        }
        return list;
    }
    // list1 的改造
    @RequestMapping("/list11")
    public List<AccountDetails> list11(@RequestBody DetailList detailList) {
        List<AccountDetails> detailsList = interfaceService.list1(detailList);
        return detailsList;
    }

    @RequestMapping("/list2")
    public List<Map<String,Object>> list2(@RequestBody AccountDetails details){
        List<Map<String,Object>> list = new ArrayList<>();

        List<AccountDetails> detailsList = interfaceService.list2(details);
        for(AccountDetails detail:detailsList){
            Map<String,Object> map = new HashMap<>();
            map.put("TRAN_DT",detail.getTranDt());
            map.put("TRAN_SEQ",detail.getTranSeq());
            map.put("CUST_NO",detail.getCustNo());
            map.put("OPP_ACC_NO",detail.getOppAccNo());
            map.put("TRAN_AMT",detail.getTranAmt());
            map.put("CCY",detail.getCcy());
            map.put("MEMO",detail.getMemo());
            map.put("TRAN_TYPE",detail.getTranType());
            map.put("TRAN_STS",detail.getTranSts());
            list.add(map);
        }
        return list;
    }

    @RequestMapping("/accountOpenList")
    public List<Map<String,Object>> selectByDate(@RequestBody Map<String,String> dateMap){
        List<Map<String,Object>> list = new ArrayList<>();
        List<AccountInfo> accountInfos = interfaceService.selectByDate(dateMap);
        //用户表现有用户数
        int openCount = interfaceService.infoCount();
        for(AccountInfo info:accountInfos){
            Map<String,Object> map = new HashMap<>();
            map.put("CHANNEL_CODE",info.getChannelCode());
            map.put("BRC_NO",info.getBrcNo());
            map.put("OPER_NO",info.getOperNo());
            map.put("OPN_TOTAL",openCount);
            list.add(map);
        }
        return list;
    }
    @RequestMapping("areaWithAmt/{DATE_START}/{DATE_END}")
    public List<Map<String,Object>> areaWithAmt(
            @PathVariable("DATE_START") String dateStart,
            @PathVariable("DATE_END") String dateEnd) {

        List<Map<String,Object>> list = new ArrayList<>();
        List<AccountDetails> detailsList = interfaceService.selectAreaListByDate(dateStart, dateEnd);
        for(AccountDetails detail:detailsList){
            Map<String,Object> map = new HashMap<>();
            map.put("AREA_CODE",detail.getAreaCode());
            map.put("TRAN_AMT",detail.getTranAmt());
            list.add(map);
        }
        return list;
    }


    //通过地址栏url传递id(不是json)
    @GetMapping("/update/{id}")
    public String update(@PathVariable("id") Integer id){

        AccountInfo info = new AccountInfo();
        info.setAccId(id);
        //info.setOperNo("00000");
        info.setOperNo("086285");
        interfaceService.update(info);
        return "success";
    }

    @PostMapping ("/update2")
    //@RequestBody的解析有两个条件：
    // 1.POST请求中content的值必须为json格式（存储形式可以是字符串，也可以是byte数组）；
    // 2.@RequestBody注解的参数类型必须是完全可以接收参数值的类型，比如：Map，JSONObject，或者对应的JavaBean；
    public String update2(@RequestBody Integer id){ //所以这种会报错需要改成下面的 切使用postman访问不是json传递，而是form-data
//    public String update2(@RequestParam Integer id){

        AccountInfo info = new AccountInfo();
        info.setAccId(id);
        //info.setOperNo("00000");
        info.setOperNo("086285");
        interfaceService.update(info);
        return "success";
    }



}
